#!/usr/bin/env python

# Copyright 2011 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Street Identity Demo Relying Party Application."""

import fix_path
from google.appengine.dist import use_library
use_library('django', '1.2')

import sys
sys.modules['ssl'] = None
from lib import httplib2
import httplib
import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
import logging
from UserString import MutableString

from lib import http, authn
from lib.loggable import loggable
import app_params as application_params
import account
import account_setup
from lib import utils

from lib import oauth2
from lib.oauth2client.client import OAuth2WebServerFlow

__author__ = "Maciej Machulak"
__maintainer__ = "Maciej Machulak"
__email__ = "mmachulak@google.com"

__copyright__ = "Copyright 2011, Google Inc."
__license__ = "Apache License 2.0"
__version__ = "0.1"
__status__ = "Prototype"

class MainHandler(webapp.RequestHandler):

    @loggable('main page') 
    def get(self, *args):
        values = {}
        path = os.path.join(os.path.dirname(__file__), 'templates/index.html')
        self.response.out.write(template.render(path, values))

class SignInHandler(webapp.RequestHandler):
    
    """Redirects the user to the correct identity provider."""
    @loggable('sign in')         
    def get(self):
      redirect_uri = application_params.IDPConfig.redirect_uri
      authorize_url = application_params.IDP_FLOW.step1_get_authorize_url(redirect_uri)
      self.redirect(authorize_url)
          
class SignOutHandler(webapp.RequestHandler):
    
    @authn.login_required
    @loggable('sign out')     
    def get(self):
        session = self.session
        session.delete()
        logging.debug('session deleted')
        self.response.headers.add_header(http.Header.set_cookie, application_params.Http.SESSION_ID + '=')
        self.redirect('/')    

routes = [('/', MainHandler),
          ('/sign_in', SignInHandler),          
          ('/sign_out', SignOutHandler),

          ('/setup', account_setup.SetupHandler),
          ('/setup/get_address', account_setup.GetAddressHandler),

          ('/account', account.AccountHandler),
          ('/account/delete', account.DeleteAddressHandler),
          
          ('/oauth/redirect', oauth2.OAuthHandler)]  
       
application = webapp.WSGIApplication(routes, debug=True)

def main():
    logging.getLogger().setLevel(logging.DEBUG)  
    run_wsgi_app(application)

if __name__ == "__main__":
    main()
